{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch4.3  编程实现ID3算法\n",
    "试编程实现基于信息熵进行划分选择的决策树算法，并为表4.3中数据生成一棵决策树。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 课后习题4.3 使用表4.3\n",
    "这里对于连续属性直接取中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from ch4 import TreeGenerate, print_tree,TreeGenerate_preprune, TreeGenerate_postprune, print_rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "homework 4.3 using table 4.3\n",
      "\n",
      "\n",
      "texture=distinct \n",
      "\n",
      "    root=stiff    0\n",
      "\n",
      "    root=curl_up    1\n",
      "\n",
      "    root=little_curl_up \n",
      "\n",
      "        sugar_ratio>0.2370    0\n",
      "\n",
      "        sugar_ratio<=0.2370    1\n",
      "\n",
      "texture=little_blur \n",
      "\n",
      "    touch=soft_stick    1\n",
      "\n",
      "    touch=hard_smooth    0\n",
      "\n",
      "texture=blur    0"
     ]
    }
   ],
   "source": [
    "# 0表示坏瓜，1表示好瓜\n",
    "# 基于基尼指数生成决策树\n",
    "dataset = pd.read_csv('data/table_4_3_watermelon_3_0.csv')\n",
    "dataset = dataset.drop('Idx',axis=1)\n",
    "\n",
    "nd = TreeGenerate(dataset)\n",
    "print(\"homework 4.3 using table 4.3\")\n",
    "print_tree(nd, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图4.3 4.4 ，使用表4.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p77 pic 4.3, pic 4.4\n",
      "\n",
      "texture=distinct \n",
      "\n",
      "    root=stiff    0\n",
      "\n",
      "    root=curl_up    1\n",
      "\n",
      "    root=little_curl_up \n",
      "\n",
      "        color=dark_green    1\n",
      "\n",
      "        color=black \n",
      "\n",
      "            touch=soft_stick    0\n",
      "\n",
      "            touch=hard_smooth    1\n",
      "\n",
      "texture=little_blur \n",
      "\n",
      "    touch=soft_stick    1\n",
      "\n",
      "    touch=hard_smooth    0\n",
      "\n",
      "texture=blur    0"
     ]
    }
   ],
   "source": [
    "dataset = pd.read_csv('data/table_4_1_watermelon_2_0.csv')\n",
    "dataset = dataset.drop('Idx',axis=1)\n",
    "\n",
    "nd = TreeGenerate(dataset)\n",
    "print(\"p77 pic 4.3, pic 4.4\", end=\"\")\n",
    "print_tree(nd, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图4.5 基于表4.2的训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "picture 4.5 using table 4.2\n",
      "\n",
      "\n",
      "color=dark_green \n",
      "\n",
      "    knocks=heavily    0\n",
      "\n",
      "    knocks=clear    0\n",
      "\n",
      "    knocks=little_heavily    1\n",
      "\n",
      "color=black \n",
      "\n",
      "    root=curl_up    1\n",
      "\n",
      "    root=little_curl_up \n",
      "\n",
      "        texture=distinct    0\n",
      "\n",
      "        texture=little_blur    1\n",
      "\n",
      "color=light_white    0"
     ]
    }
   ],
   "source": [
    "# 表4.2 图4.5\n",
    "# 这里和书上的图不一样，是因为色泽和脐部二者在训练集上的分布一样，python的浮点运算存在误差，体现在gainD计算时的不同。\n",
    "dataset = pd.read_csv('data/table_4_1_watermelon_2_0.csv')\n",
    "dataset = dataset.drop('Idx',axis=1)\n",
    "train_dataset = dataset.ix[[0,1,2,5,6,9,13,14,15,16],:]\n",
    "nd = TreeGenerate(train_dataset)\n",
    "print(\"picture 4.5 using table 4.2\")\n",
    "print_tree(nd, 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:anaconda]",
   "language": "python",
   "name": "conda-env-anaconda-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
